<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="仓库" prop="warehouseCode">
        <el-select v-model="queryParams.warehouseCode" placeholder="请选中仓库" clearable filterable @change="warehouseChange" @keyup.enter.native="handleQuery">
          <el-option v-for="item in warehouseList" :key="item.warehouseId" :label="item.warehouseName" :value="item.warehouseId" />
        </el-select>
      </el-form-item>

      <el-form-item label="库位" prop="areaId">
        <el-select v-model="queryParams.areaCode" placeholder="请选中库位" clearable filterable @keyup.enter.native="handleQuery">
          <el-option v-for="item in areaList" :key="item.areaId" :label="item.areaName" :value="item.areaId" />
        </el-select>
      </el-form-item>
      <el-form-item label="物料编码" prop="itemCode">
        <el-input v-model="queryParams.itemCode" placeholder="请输入物料编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['wms:sluggish:export']">导出</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange" height="calc(100vh - 230px)">
      <el-table-column type="index" label="序号" align="center" width="50" />
      <el-table-column label="物料编码" width="160px" align="center" prop="itemCode" />
      <el-table-column label="物料名称" width="200px" align="center" prop="itemName" />
      <el-table-column label="仓库" width="100px" align="center" prop="warehouseName" />
      <el-table-column label="库位" width="100px" align="center" prop="areaName" />
      <el-table-column label="预警内容" align="center" width="200px" prop="msg" />
      <el-table-column label="预警次数" width="100px" align="center" prop="cnt" />
      <el-table-column label="处理人" width="100px" align="center" prop="updateBy" />
      <el-table-column label="处理状态" width="100px" align="center" prop="handleFlag" />
      <el-table-column label="预警时间" width="100px" align="center" prop="createTime" />
      <el-table-column label="处理时间" width="100px" align="center" prop="updateTime">
        <template slot-scope="scope">
          <span>{{ scope.row.handleFlag == '已处理' ? scope.row.updateTime : "" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="处理备注" width="100px" align="center" prop="remark"></el-table-column>
      <el-table-column label="操作" width="100px" align="center">
        <template slot-scope="scope">
          <el-button type="text" :disabled="scope.row.handleFlag == '已处理'" size="mini" @click="handleWarn(scope.row)" icon="el-icon-zoom-in" v-hasPermi="['wms:sluggish:handle']">处理</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.current" :limit.sync="queryParams.size" @pagination="getList" />

    <el-dialog :visible.sync="showHandleDialog" center title="预警处理" @close="cancelHandleDialog">
      <el-input type="textarea" v-model="remark" placeholder="请输入处理备注" />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmHandle">确 定</el-button>
        <el-button @click="cancelHandleDialog">取 消</el-button>
      </div>
    </el-dialog>
    <!-- </el-col>
      </el-row> -->
  </div>
</template>

<script>
import { list, handleWarn, exportWarn } from "@/api/mes/wm/wmWarehouseInventoryWarn";
import Treeselect from "@riophae/vue-treeselect";
import { listWarehouse } from "@/api/mes/wm/warehouse";
import { listArea } from "@/api/mes/wm/area"
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { title } from "process";
export default {
  name: "WmWarehouseInventoryWarn",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      showHandleDialog: false,
      selectedId: null,
      // 显示搜索条件
      showSearch: true,
      itemTypeName: null,
      areaList: [],
      remark: null,
      // 总条数
      total: 0,
      //物料产品分类树
      itemTypeOptions: undefined,
      dataList: [],
      warehouseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        itemCode: null,
        itemName: null,
        locationCode: null,
        warehouseCode: null
      },

    };
  },

  created() {
    this.getList();
    this.getWarehouseList();
  },
  methods: {
    cancelHandleDialog() {
      this.showHandleDialog = false;
      this.selectedId = null;
      this.remark = null;
    },
    handleWarn(row) {
      this.showHandleDialog = true;
      this.selectedId = row.id;
    },
    confirmHandle() {
      if (this.remark && this.remark.length > 255) {
        this.$message({
          type: 'warning',
          message: '处理备注长度不能超过255个字符!'
        });
        return;
      }
      this.$confirm('确定已经处理了这个预警?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return handleWarn({ id: this.selectedId, remark: this.remark })
      }).then(response => {
        this.$message({
          type: 'success',
          message: '处理成功!'
        });
        this.cancelHandleDialog();
        this.getList();
      }).catch(() => { });
    },
    warehouseChange(val) {
      console.log("val", val);
      this.queryParams.warehouseId = val;
      this.getAreaList();
      this.queryParams.areaId = null;
    },
    getWarehouseList() {
      listWarehouse({ pageSize: 999 }).then(response => {
        this.warehouseList = response.rows
      })
    },
    getAreaList() {
      listArea({ wareHouseId: this.queryParams.warehouseId }, { "no-page": true }).then(response => {
        this.areaList = response.rows
      })
    },
    /** 查询库存记录列表 */
    getList() {
      this.loading = true;
      list(this.queryParams).then(response => {
        this.dataList = response.rows
        this.total = response.total;
        this.loading = false;
      });
    },

    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.itemTypeId = data.id;
      this.handleQuery();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.materialStockId)
      this.queryParams.wmMaterialStockIdList = selection.map(item => item.materialStockId);
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('/mes/wm/warehouse_inventorywarn/export', {
        ...this.queryParams
      }, `wmWarehouseInventoryRetentionWarn_${new Date().getTime()}.xlsx`, null, "get")
    }
  }
};
</script>